package com.jzy.bxb.model.mongo.dao;

import org.mongodb.morphia.dao.BasicDAO;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;

import com.jzy.bxb.core.db.mongo.AbsMongoManager;
import com.jzy.bxb.model.mongo.entity.ServerInfo;

public class ServerInfoDao extends BasicDAO<ServerInfo, Integer> {
	private static volatile ServerInfoDao serverInfoDao;

	public ServerInfoDao(AbsMongoManager mongoManager) {
		super(ServerInfo.class, mongoManager.getMongoClient(), mongoManager.getMorphia(),
				mongoManager.getMongoConfig().getDbName());
	}

	public static ServerInfoDao init(AbsMongoManager mongoManager) {
		if (serverInfoDao == null) {
			synchronized (UserDao.class) {
				if (serverInfoDao == null) {
					serverInfoDao = new ServerInfoDao(mongoManager);
				}
			}
		}
		return serverInfoDao;
	}

	/**
	 * 获取增加用户ID号
	 * 
	 * @return
	 */
	public static long incUserNum() {
		Query<ServerInfo> query = serverInfoDao.createQuery().filter("id", 1);
		UpdateOperations<ServerInfo> operations = serverInfoDao.createUpdateOperations().inc("userNum");
		ServerInfo serverInfo = serverInfoDao.getDs().findAndModify(query, operations);
		if (serverInfo == null) {
			serverInfo = new ServerInfo();
			serverInfoDao.save(serverInfo);
			serverInfo = serverInfoDao.getDs().findAndModify(query, operations);
		}
		return serverInfo.getUserNum();

	}

}
